package cn.blue.org.controller;
import cn.blue.base.annotation.PreAuthorize;
import cn.blue.base.config.BussinessException;
import cn.blue.base.query.BaseQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.PageList;
import cn.blue.org.domain.Department;
import cn.blue.org.domain.Employee;
import cn.blue.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartController {
    @Autowired
    private DepartmentService departmentService;

    //查询所有
    @ResponseBody
    @RequestMapping("/list")
    public List<Department> findAll() {
        List<Department> all = departmentService.findAll();
        return all;


    }

    //删除
    @PreAuthorize(name = "删除",sn = "department:delete")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        if(id==null){
            throw new BussinessException("参数不能为空");
        }
        else {
            departmentService.del(id);
            return AjaxResult.me().setSuccess(true).setMsg("请求成功");
        }

    }

    //新增或者修改
    @PreAuthorize(name = "新增修改",sn = "department:addOrUpdate")
    @ResponseBody
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        System.out.println(department);
        if (null == department.getId()) {
            departmentService.add(department);
        }else {
            departmentService.update(department);
        }
        return AjaxResult.me().setSuccess(true).setMsg("请求成功");
    }


    @PreAuthorize(name = "分页查询",sn = "department:list")
    @ResponseBody
    @RequestMapping
    public PageList<Department>queryPage(@RequestBody BaseQuery query){
        PageList<Department> list = departmentService.queryByPage(query);
        return list;


    }


    //批量删除
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long>ids){
        System.out.println(ids);
        departmentService.batchDel(ids);
        return AjaxResult.me().setSuccess(true).setMsg("删除成功");

    }


    @PreAuthorize(name = "根据id查询",sn = "department:get")
    @ResponseBody
    @RequestMapping("/emps")
    public List<Employee> findByDId(@RequestBody Department department){
        Long id = department.getId();
        System.out.println(id+"我在这人");
        return departmentService.findByDId(id);
    }

}

